package com.skycode7.servlet;

import com.skycode7.bean.NewsBean;
import com.skycode7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/newsEdit.jsp")
public class NewsEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String newsID =req.getParameter("NewsID");
        String newsTitle = req.getParameter("NewsTitle");
        String newsContent = req.getParameter("NewsContent");
        NewsBean newsBean = new NewsBean();
        newsBean.setNewsID(Integer.parseInt(newsID));
        newsBean.setNewsTitle(newsTitle);
        newsBean.setNewsContent(newsContent);
        newsBean.setNewsTime((new Date()).toString());
        newsBean.setAdminName("admin");
        int result = (new NewsDao()).updateNews(newsBean);
        if(result >0){
            resp.sendRedirect("news.html");
        }else{
            req.getRequestDispatcher("newsEdit.jsp").forward(req, resp);
        }
    }

    private HttpServletRequest setCharacterEncoding(String s) {
        return null;
    }
}
